Vue.createApp({
    data() {
        return {
            changeHint: '',
            changeType: 'pwd',
            basicInfo: {
                name: '',
                type: ''
            },
            changeInfo: {
                name: '',
                newPwd: ''
            },
            oldPwd: '',
            againPwd: ''
        }
    },
    methods: {
        changeName() {
            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]+$")).test(this.changeInfo.name) || this.changeInfo.name.length <= 0 || this.changeInfo.name.length > 20) {
                this.changeHint = "名字不应该包含特殊字符和空格且长度在1至20";
                return;
            }
            if (this.basicInfo.name === this.changeInfo.name) {
                this.changeHint = "新名字与旧名字一样，不需要修改！";
                return;
            }
            console.log(this.changeInfo.name);
            axios.post("/change/info", this.changeInfo.name).then(resp => {
                if (resp.data === "OK") {
                    this.changeHint = "修改成功！";
                    this.basicInfo.name = this.changeInfo.name;
                } else {
                    this.changeHint = "未知错误导致失败！";
                }
            }).catch(ex => {
                this.changeHint = "未知错误导致失败！";
            });
        },
        changePwd() {
            let reg = new RegExp("[a-zA-Z\\d#*+._-]{6,18}");

            if (!reg.test(this.oldPwd)) {
                this.changeHint = "请正确输入旧密码(6-18位, 允许特殊符号#*+._-)";
                return;
            }
            if (!reg.test(this.changeInfo.newPwd)) {
                this.changeHint = "请正确输入新密码(6-18位, 允许特殊符号#*+._-)";
                return;
            }
            if (!reg.test(this.againPwd)) {
                this.changeHint = "请正确输入第二次新密码(6-18位, 允许特殊符号#*+._-)";
                return;
            }

            if (this.changeInfo.newPwd !== this.againPwd) {
                this.changeHint = "您输入的第一次新密码与第二次新密码不一致";
                return;
            }

            if (this.changeInfo.newPwd === this.oldPwd) {
                this.changeHint = "您输入的新密码与旧密码一致，无需修改";
                return;
            }
            console.log(this.changeInfo.newPwd);
            axios.post("/change/pwd", this.changeInfo.newPwd).then(resp => {
                if (resp.data === "OK") {
                    alert("修改成功，即将退出登录");
                    window.location.href = "/quit";
                } else {
                    let data = resp.data.split(":");
                    if (data.length > 1 && data[1] === "OldPwd_Inconsistent") {
                        this.changeHint = "旧密码错误！";
                    } else {
                        this.changeHint = "未知错误导致失败！";
                    }
                }
            }).catch(ex => {
                this.changeHint = "未知错误导致失败！";
            });

        }
    },
    mounted() {
        axios.get("/change/get").then(resp => {
            this.basicInfo = resp.data;
        });
    },
    watch: {
        changeType(n, o) {
            this.changeHint = '';
            this.changeInfo = {
                name: '',
                newPwd: ''
            };
            this.oldPwd = '';
            this.againPwd = '';
        }
    }
}).mount("#change-app");